package person.bangbang.im.Androidgin.Framework;

import java.util.ArrayList;
import java.util.List;

/**
 * a abstract protocol which describe a IM protocol.
 * a concrete protocol MUST inherits this.
 * 
 * all code in this class execute in mainUIThread, so any loong dealing-with 
 * should be go their Thread.
 * 
 * usually, you should not use this class directly instead of Account (in FrameWork)
 * of course, if you implement a new IM protocol, you inherits this.
 * 
 * @author bangbang.song@gmail.com
 */
public abstract class Protocol {
	protected String mUserName;
	protected String mPassward;
	protected StatusType mLoginType;
	
	private List<OnLoginProcess> mLoginProcessListeners = new ArrayList<OnLoginProcess>();
	
	public Protocol(String userName, String passward, StatusType type) {
		mUserName = userName;
		mPassward = passward;
		mLoginType = type;
	}
	
	public String getUserName() {
		return mUserName;
	}
	public String getPWD() {
		return mPassward;
	}
	public StatusType getLoginType() {
		return mLoginType;
	}
	// Maj.Min.CodeVer  e.g 1.0.12
	public abstract String getProtocolID();
	
	/**
	 * 
	 * @param user_name
	 * @param passward
	 * @return true if all is OK, if you want to get the step notification
	 *         you should make use of OnLoginProcess interface.
	 */
	public abstract boolean signon();
	public abstract boolean signoff();
	
	public abstract Buddy getMyself();
//	public abstract List<IBuddy> getBuddies();
	
	public abstract boolean sendIm(Message msg);
	
	public void regOnLoginProcessListener(OnLoginProcess l) {
		mLoginProcessListeners.add(l);
	}
	public void unRegOnLoginProcessListener(OnLoginProcess l) {
		mLoginProcessListeners.remove(l);
	}
	protected void dispatchLoginStep(int which_step, int total_steps, String msg) {
		for (OnLoginProcess l : mLoginProcessListeners) {
			l.onLoginStep(which_step, total_steps, msg);
		}
	}
	protected void dispatchLoginErr(int code, String msg) {
		for (OnLoginProcess l : mLoginProcessListeners) {
			l.onLoginErr(code, msg);
		}
	}
	
	public interface OnLoginProcess {
		public void onLoginStep(int which_step, int total_steps, String msg);
		public void onLoginErr(int code, String msg);
	}

}
